<?php
class Dir {

    // user data
    var $id = 0; // ID of the selected directory
	var $parent      = 0; // parent ID of the selected Directory
	var $name = "";
	var $language = "";
	var $created = "";
	var $darkColor   = "#64b5db";
	var $lightColor  = "#badeef";
	var $noneColor   = "#ffffff";


    function init($id = 0) {
        global $admin;
	    
	    $str = "";
	    if ($id == 0)
	    {
	        $this->language = $admin->language;
	    }
	    elseif ($this->validDir($id, $admin->language))
	    {
	        $this->getDirData($id, $admin->language);
	    }
	    else
	    {
	        $str.= $this->error(_WRONGDIRECTORYDATA);
		    $this->language = $admin->language;
	    }
	    
	    return $str;
    }


    function getDirData($id, $language) {
        global $table,$db;
	    
	    $result = $db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$language'");
	    if ($db->numRows($result > 0))
	    {
	    	$row = $db->nextRecord ($result);
	    	$this->id       = $row->id;
	    	$this->parent   = $row->parent;
	    	$this->name     = $row->name;
	    	$this->language = $row->language;
	    }
    }


	function validDir ($id, $language) {
	    global $table,$db;
		
		if ($db->numRows($db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$language'")) == 0)
		    { return false; }
		else
		{
		    return true;
		}
	}


    function getRoute($id = 0) {
	    global $table,$db,$admin;
	    
	    $str = "";
	    $result = $db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$this->language'");
	    if ($db->numRows($result) > 0)
        {
	    	$row = $db->nextRecord($result);
	    	$str.= $this->getRoute($row->parent)."<a href='".GLOBAL_SELF."?op=d&id=$row->id'><b>".$row->name."</b></a>/";
	    }
	    
	    return $str;
	}


    function go($id = 0) {
        global $table,$db,$html,$lang,$html_form;
    
        $str = "";
		
		$str.= $html->center(
                   "<b>"._CONTENTOF.
				   " /<a href='".GLOBAL_SELF."?op=d&id=0'><b>ROOT</a>/".
				   $this->getRoute($id).
				   "<br><br>"
			   ,"adminmenu");
		$str.=
		    $html_form->Begin(GLOBAL_SELF."?op=d&id=$id","post","filemanager").
		    $html->table(
		        $this->printTable(
			        $this->getDirContent($id)
			    )
		    ,"100%",0,0,0);
		$str.= "<br><br>".
		    $html->table(
				$html->th(_COMMANDBOX).
				$html->tr(
				    $html->td(
				        $html_form->addText("target","",40,255)
					,1)
				).
				$html->tr(
				    $html->td(
						$html_form->addSubmit("submit",_EXECUTE)
					,1)
				)
		    ,"",1,5,0,"","1px_border").
			$html_form->End();
		
		return $str;
    }


    function getDirContent($id = 0) {
	    global $table,$db,$html;
		
		$i = 0;
		$str = "";
		$row = $db->nextRecord($db->query("SELECT * FROM $table->dir WHERE id='$id' AND language='$this->language'"));
		$parent = $row->parent;
		
		if ($id > 0) {
		    $str.=
		    $html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->lightColor).
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->noneColor).
			    $html->td(
	                "<a href='".GLOBAL_SELF."?op=d&id=$parent'><img src='admin/images/dir.gif' border='0' width='14' height='11'></a>"
                    ,0,$this->darkColor
			    ).
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("..",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->noneColor).
			    $html->td("&nbsp;",0,$this->darkColor).
			    $html->td("&nbsp;",0,$this->lightColor).
			    $html->td("&nbsp;",0,$this->darkColor)
		    ,0,16);
		    $i++;
		}
		
		$result = $db->query("SELECT * FROM $table->dir WHERE parent='$id' AND language='$this->language' ORDER BY id");
		    while ($row = $db->nextRecord($result))
		    {
			    $str.= $this->showDir($row->name, $row->created, $row->id, $i);
				$i = 1-$i;
			}
		$result = $db->query("SELECT * FROM $table->page WHERE dir='$id' AND language='$this->language' ORDER BY id");
		    while ($row = $db->nextRecord($result))
		    {
			    $str.= $this->showPage($row->name, $row->created, $row->id, $i);
				$i = 1-$i;
			}
		
		return $str;
	}


	function showDir($name, $created, $id = 0, $i) {
	    global $html,$html_form;
		
		if ($i == 0) { $bgColor = $this->darkColor; } else { $bgColor = $this->lightColor; }
		
		$str =
		$html->tr(
			$html->td("&nbsp;",0,$this->darkColor).
			$html->td("&nbsp;",0,$this->lightColor).
			$html->td("&nbsp;",0,$this->darkColor).
			$html->td("&nbsp;",0,$this->noneColor).
			$html->td(
			    "<a href='".GLOBAL_SELF."?op=d&id=$id'><img src='admin/images/dir.gif' border='0' width='14' height='11'></a>"
                ,0,$bgColor
			).
			$html->td("&nbsp;",0,$bgColor).
			$html->td($name,0,$bgColor).
			$html->td("&nbsp;",0,$bgColor).
			$html->td($html->time2str($created),0,$bgColor).
			$html->td("&nbsp;",0,$this->noneColor).
			$html->td($html_form->addRadio("action","d:delete_all:$id",false,"radio"),0,$this->darkColor).
			$html->td($html_form->addRadio("action","d:rename:$id",false,"radio"),0,$this->lightColor).
			$html->td("&nbsp;",0,$this->darkColor)
		,0,16);
		
		return $str;
	}


	function showPage($name, $created, $id = 0 ,$i) {
	    global $html,$html_form;
		
		if ($i == 0) { $bgColor = $this->darkColor; } else { $bgColor = $this->lightColor; }
		
		$str =
		$html->tr(
			$html->td($html_form->addRadio("action","p:edit:$id",false,"radio"),0,$this->darkColor).
			$html->td($html_form->addRadio("action","p:rename:$id",false,"radio"),0,$this->lightColor).
			$html->td($html_form->addRadio("action","p:delete:$id",false,"radio"),0,$this->darkColor).
			$html->td("&nbsp;",0,$this->noneColor).
			$html->td("",0,$bgColor).
			$html->td("&nbsp;",0,$bgColor).
			$html->td($name,0,$bgColor).
			$html->td("&nbsp;",0,$bgColor).
			$html->td($html->time2str($created)." (".$id.")",0,$bgColor).
			$html->td("&nbsp;",0,$this->noneColor).
			$html->td("&nbsp;",0,$this->darkColor).
			$html->td("&nbsp;",0,$this->lightColor).
			$html->td("&nbsp;",0,$this->darkColor)
		,0,16);
		
		return $str;
	}


    function printTable($text = "") {
	    global $html,$html_form;
		
		$str =
		    $html->tr(
			    $html->td("&nbsp;",0,$this->darkColor,"",20).
				$html->td("&nbsp;",0,$this->darkColor,"",20).
				$html->td("&nbsp;",0,$this->darkColor,"",20).
				$html->td("&nbsp;",0,$this->darkColor,"",2).
				$html->td("&nbsp;",0,$this->darkColor,"",14).
				$html->td("&nbsp;",0,$this->darkColor,"",2).
				$html->td("<b>"._EDIT."</b>",0,$this->darkColor,"adminmenu",350).
				$html->td("&nbsp;",0,$this->noneColor,"",60).
				$html->td("<b>"._CREATENEWDIRECTORY."</b>",2,$this->darkColor,"adminmenu",350).
				$html->td("&nbsp;",0,$this->darkColor,"",2).
				$html->td($html_form->addRadio("action","d:new:$this->id",false,"radio"),0,$this->darkColor,"",20).
				$html->td("&nbsp;",0,$this->darkColor,"",20).
				$html->td("&nbsp;",0,$this->darkColor,"",20)
			).
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("<b>"._RENAME."</b>",0,$this->lightColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<b>"._RENAME."</b>",2,$this->lightColor,"adminmenu").
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			).
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("<b>"._DELETE."</b>",0,$this->darkColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<b>"._DELETEWITHCONTENT."</b>",2,$this->darkColor,"adminmenu").
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			).
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<span class='grey'>"._NAME."</span>",0,$this->noneColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<span class='grey'>"._CREATED."</span>",0,$this->noneColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			,0,40).
			$text.
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			,1,20).
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("<b>"._DELETE."</b>",0,$this->darkColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<b>"._DELETEWITHCONTENT."</b>",2,$this->darkColor,"adminmenu").
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			).
			$html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("<b>"._RENAME."</b>",0,$this->lightColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("<b>"._RENAME."</b>",2,$this->lightColor,"adminmenu").
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->lightColor).
				$html->td("&nbsp;",0,$this->darkColor)
			).
			 $html->tr(
			    $html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("<b>"._EDIT."</b>",0,$this->darkColor,"adminmenu").
				$html->td("&nbsp;",0,$this->noneColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor).
				$html->td("&nbsp;",0,$this->darkColor)
			);
		
		return $str;
	}


	function done($text) {
	    return "<span class='green'><b>".$text."</b></span>";
	}


	function error($text) {
	    return "<span class='red'><b>".$text."</b></span>";
	}


    function createNewDirectory($name) {
	    global $table,$db;
		
		if ($name == "")
		{ // no name entered
		    return $this->error(_NODIRECTORYNAME);
		}
		elseif ($db->numRows($db->query("SELECT * FROM $table->dir WHERE name='$name' AND parent='$this->id' AND language='$this->language'")) > 0)
		{ // such directory name does already exist
		    return $this->error(_NEWDIRECTORYNAMEEXISTS);
		}
		else
		{ // executing the action - create new directory
		    if ($db->query("INSERT INTO $table->dir VALUES('','$this->id','$name','$this->language','".date('YmdHis')."')"))
			{ // new directory was created
			    return $this->done(_NEWDIRECTORYCREATED);
			}
			else
			{ // action failed
			    return $this->error(_ACTIONFAILED);
			}
		}
	}


    function renameDirectory($newname) {
	    global $table,$db;
		
		    if ($newname == "")
		    { // no name entered
		        return $this->error(_NODIRECTORYNAME);
		    }
		    elseif ($db->numRows($db->query("SELECT * FROM $table->dir WHERE name='$newname' AND parent='$this->parent' AND language='$this->language'")) > 0)
		    { // such directory name does already exist
		        return $this->error(_NEWDIRECTORYNAMEEXISTS);
		    }
		    else
		    { // executing the action - rename directory
		        if ($db->query("UPDATE $table->dir SET name='$newname' WHERE id='$this->id' AND language='$this->language'"))
			    { // new directory was renamed
			        return $this->done(_DIRECTORYRENAMED);
			    }
			    else
			    { // action failed
			        return $this->error(_ACTIONFAILED);
			    }
		    }
	}


    function deleteDirectory() {
	    global $table,$db;
		
		if ($this->validDir($this->id, $this->language))
		{ // executing the action - rename directory
            if ($db->query("DELETE FROM $table->dir WHERE id='$this->id' AND language='$this->language'"))
		    { // new directory was deleted
			    $db->query("DELETE FROM $table->page WHERE dir='$this->id' AND language='$this->language'");
			    $this->deleteChildContent($this->id);
		        return $this->done(_DIRECTORYDELETED);
		    }
		    else
		    { // action failed
		        return $this->error(_ACTIONFAILED);
		    }
		}
	}


    function deleteChildContent($parent) {
	    global $table,$db;
		
        $result = $db->query("SELECT * FROM $table->dir WHERE parent='$parent' AND language='$this->language'");
		    while ($row = $db->nextRecord($result)) // go through every child and delete it and its children
	    	{
		        $this->deleteChildContent($row->id); // recursion - deleting children of the children
				$db->query("DELETE FROM $table->dir WHERE id='$row->id' AND language='$this->language'"); // delete the directory
				$db->query("DELETE FROM $table->page WHERE dir='$row->id' AND language='$this->language'"); // delete pages
	    	}
	}

}
?>